<template>
    <div class="personal-container">
      <el-card class="box-card">
        <template #header>
          <div class="card-header">
            <span class="title">个人信息</span>
          </div>
        </template>
  
        <div class="profile-container">
          <!-- 左侧头像区域 -->
          <div class="avatar-section">
            <el-upload
              class="avatar-uploader"
              action="/api/upload"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <el-avatar :size="120" :src="userInfo.avatar">
                <img
                  src="https://cube.elemecdn.com/e/fd/0fc7d20532fdaf769a25683617711png.png"
                  alt="默认头像"
                />
              </el-avatar>
              <div class="upload-mask">
                <el-icon><Camera /></el-icon>
              </div>
            </el-upload>
            <div class="user-name">{{ userInfo.username }}</div>
            <!-- <div class="user-role">{{ userInfo }}</div> -->
          </div>
  
          <!-- 右侧信息区域 -->
          <div class="info-section">
            <el-descriptions title="基本信息" :column="1" border>
              <el-descriptions-item label="用户名">
                {{ userInfo.username }}
              </el-descriptions-item>
              <el-descriptions-item label="邮箱">
                {{ userInfo.email || '-' }}
              </el-descriptions-item>
              <el-descriptions-item label="手机号">
                {{ userInfo.phone || '-' }}
              </el-descriptions-item>
              <el-descriptions-item label="最后登录时间">
                {{ userInfo.update_time }}
              </el-descriptions-item>
            </el-descriptions>
  
            <div class="action-buttons">
              <el-button type="primary" @click="showPasswordDialog">
                修改密码
              </el-button>
            </div>
          </div>
        </div>
      </el-card>
  
      <!-- 修改密码对话框 -->
      <el-dialog v-model="passwordDialogVisible" title="修改密码" width="500px">
        <el-form
          :model="passwordForm"
          :rules="passwordRules"
          ref="passwordFormRef"
          label-width="100px"
        >
          <el-form-item label="原密码" prop="old_password">
            <el-input
              v-model="passwordForm.old_password"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="新密码" prop="password">
            <el-input
              v-model="passwordForm.password"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="password_confirm">
            <el-input
              v-model="passwordForm.password_confirm"
              type="password"
              show-password
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <span class="dialog-footer">
            <el-button @click="passwordDialogVisible = false">取消</el-button>
            <el-button type="primary" @click="submitPasswordForm">
              确认修改
            </el-button>
          </span>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup>
  import { ref, reactive } from 'vue'
  import { Camera } from '@element-plus/icons-vue'
  import { useAuthStore } from '@/stores/auth.ts'
  import { changePassword } from '@/api/admin'
  import {ElMessage} from "element-plus";

  const authStore = useAuthStore();

  const userInfo = authStore.userInfo;
  
  const passwordDialogVisible = ref(false)
  const passwordFormRef = ref(null)
  
  const passwordForm = reactive({
    old_password: '',
    password: '',
    password_confirm: ''
  })
  
  const passwordRules = reactive({
    old_password: [
      { required: true, message: '请输入原密码', trigger: 'blur' }
    ],
    password: [
      { required: true, message: '请输入新密码', trigger: 'blur' },
      { min: 8, message: '密码长度至少8位', trigger: 'blur' }
    ],
    password_confirm: [
      { required: true, message: '请确认密码', trigger: 'blur' },
      {
        validator: (rule, value, callback) => {
          if (value !== passwordForm.password) {
            callback(new Error('两次输入密码不一致'))
          } else {
            callback()
          }
        },
        trigger: 'blur'
      }
    ]
  })
  
  const showPasswordDialog = () => {
    passwordDialogVisible.value = true
  }

  // 提交修改密码
  const submitPasswordForm = () => {
    passwordFormRef.value.validate( async (valid) => {
      if (valid) {
        await changePassword(passwordForm)
        ElMessage.success('密码修改成功');
        passwordDialogVisible.value = false
      }
    })
  }
  
  const handleAvatarSuccess = (response) => {
    userInfo.avatar = response.url
  }
  </script>
  
  <style scoped>
  .personal-container {
    margin: 0 auto;
  }
  
  .profile-container {
    display: flex;
    gap: 40px;
    padding: 20px;
  }
  
  .avatar-section {
    text-align: center;
    flex: 0 0 200px;
  }
  
  .avatar-uploader {
    position: relative;
    margin-bottom: 20px;
  }
  
  .upload-mask {
    position: absolute;
    top: 0;
    left: 20%;
    width: 120px;
    height: 120px;
    background: rgba(0, 0, 0, 0.5);
    border-radius: 50%;
    opacity: 0;
    transition: opacity 0.3s;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    cursor: pointer;
  }
  
  .avatar-uploader:hover .upload-mask {
    opacity: 1;
  }
  
  .user-name {
    font-size: 18px;
    font-weight: 500;
    margin: 10px 0;
  }
  
  .user-role {
    color: #666;
    font-size: 14px;
  }
  
  .info-section {
    flex: 1;
  }
  
  .action-buttons {
    margin-top: 30px;
    text-align: right;
  }
  
  .title {
    font-size: 18px;
    font-weight: bold;
  }
  </style>